
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include "SpriteBatchTester.h"

SpriteBatchTester::SpriteBatchTester()
{
    fangle = 0.0f;
    for (int f=0; f<SPRITES; f++)
        sprites[f].lifeTime = -1.0f;            // inactive.
}


void SpriteBatchTester::run( float secsPassed )
{
    fangle+=secsPassed;
    bool emitted = false;
    QVector2D temp;
        // Update flying sprites
    for (int f=0; f<SPRITES; f++) {
            // Emit maximumly one particle / frame
        if (emitted == false && sprites[f].lifeTime<0) {
            sprites[f].pos = QVector2D(0,0);
            do {
                sprites[f].dir = QVector2D( (rand() & 255)-128, (rand() &255) - 128);
            } while (sprites[f].dir.x() == 0 && sprites[f].dir.y() == 0);
            sprites[f].dir.normalize();
            sprites[f].dir *= 200.0f;
            sprites[f].power = 0.0f;
            sprites[f].angle = (rand()&255)/255.0f * 3.14159f * 2.0f;
            sprites[f].angleInc = (float)((rand() & 255) ) / 40.0f;
            sprites[f].lifeTime = 0.5f + (float)(rand() & 255)/255.0f;
            sprites[f].size = ((rand() & 255)/255.0f + 0.05f) * 100.0f;
            sprites[f].r = (rand() & 255) / 512.0f + 0.5f;
            sprites[f].g = (rand() & 255) / 512.0f + 0.5f;
            sprites[f].b = (rand() & 255) / 512.0f + 0.5f;
            emitted = true;
        }

        if (sprites[f].lifeTime>0) {
            temp = sprites[f].dir *secsPassed;
            sprites[f].pos += temp;
            sprites[f].power += secsPassed;
            sprites[f].lifeTime -= secsPassed;
            sprites[f].angle += sprites[f].angleInc * secsPassed;
        }
    }
}

void SpriteBatchTester::draw( SpriteBatch *batch, int texture )
{
    SpriteDrawInfo sdi;
    sdi.textureHandle = texture;

    // Render all of the sprites
    for (int f=0;f <SPRITES; f++) {
        if (sprites[f].lifeTime>0) {
            sdi.setTargetPos( sprites[f].pos.x() + batch->getTargetWidth()/2, sprites[f].pos.y()+batch->getTargetHeight()/2);
            sdi.angle = sprites[f].angle;
            sdi.setScale( sprites[f].size);

            sdi.a = sprites[f].power * sprites[f].lifeTime * 4.0f;
            if (sdi.a > 1.0f) sdi.a = 1.0f;
            sdi.setColor(sprites[f].r, sprites[f].g, sprites[f].b, sdi.a);
            batch->draw( &sdi );
        }
    }

    sdi.setColor(1,1,1,1);
    sdi.textureHandle = texture;
    sdi.setTargetPos(0,0);
    sdi.setTargetPos( batch->getTargetWidth() / 2.0f, batch->getTargetHeight() / 2.0f);
    sdi.setScale(300.0f, 50.0f );
    sdi.angle = fangle;
    batch->draw( &sdi );

    sdi.setSourceRect(0.25 + cosf( fangle /3.0f ) *0.24f,0.25 + sinf( fangle /3.0f ) *0.24f,0.5f, 0.5f );
    sdi.angle = sinf( fangle / 2.0f );
    sdi.setScale(batch->getTargetWidth()/8,batch->getTargetHeight()/8);
    sdi.setTargetPos(batch->getTargetWidth()/4, batch->getTargetHeight()/2);
    batch->draw(&sdi);

    sdi.setSourceRect();
    sdi.angle = fangle/1.5f;
    sdi.setScale( 200.0f, 100.0f );
    sdi.setColor(0.0, 1.0f, 0.0f, 0.8f);
    sdi.setTargetPos(batch->getTargetWidth()*3/4, batch->getTargetHeight()*1/4);
    sdi.setOrigin( 0.0f, 0.5f );
    batch->draw( &sdi );

    sdi.setScale( 100.0f, 300.0f );
    sdi.setColor(1.0, 0.0f, 0.0f, 0.5f);
    sdi.setTargetPos(batch->getTargetWidth()*3/4, batch->getTargetHeight()*3/4);
    sdi.setOrigin( 0.5f, 0.0f );
    batch->draw( &sdi );
}
